package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.mes.biz.domain.MesWorkforceConfig;
import com.dnjn.mes.biz.domain.MesWorkforceXref;
import com.dnjn.mes.biz.service.IMesWorkforceConfigService;
import com.dnjn.mes.biz.service.IMesWorkforceXrefService;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.core.domain.entity.SysUser;
import com.dnjn.mes.common.utils.StringUtils;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import com.dnjn.mes.system.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import com.dnjn.mes.common.core.controller.BaseController;

/**
 * <p>
 * 班次 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-08-21
 */
@Tag(name = "MesWorkforceConfig 班次", description = "班次")
@RestController
@RequestMapping("/biz/mes-workforce-config")
public class MesWorkforceConfigController extends BaseController {

    @Autowired
    protected IMesWorkforceConfigService baseService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IMesWorkforceXrefService workforceXrefService;

    @GetMapping("/get/{workforceId}")
    public AjaxResult get(@PathVariable Long workforceId) {
        Assert.notNull(workforceId, "workforceId不能为空！");
        MesWorkforceConfig workforceConfig = baseService.getById(workforceId);
        QueryWrapper<MesWorkforceXref> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workforce_id", workforceId);
        List<MesWorkforceXref> mesWorkforceXrefList = workforceXrefService.list(queryWrapper);
        for (MesWorkforceXref mesWorkforceXref : mesWorkforceXrefList) {
            //给前端回显用户列表用
            mesWorkforceXref.setAdmin(true);
        }
        workforceConfig.setWorkforceXrefList(mesWorkforceXrefList);
        return success(workforceConfig);
    }

    /*
       {
          "workforceName": "AA",
          "startTime": "08:00:00",
          "endTime": "20:00:00",
          "workforceXrefList": [
            {
              "userId": 131,
              "userName": "test"
            },{
              "userId": 135,
              "userName": "123"
            }
          ]
        }
    */
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesWorkforceConfig domain) {
        //domain.setCreateBy(getUsername());
        boolean flage = baseService.saveWorkforceConfig(domain);
        return toAjax(flage);
    }

    /*
    *     "workforceId": 1693795874993377281,
          "workforceName": "BB",
          "startTime": "08:00:00",
          "endTime": "20:00:00",
          "workforceXrefList": [
            {
              "userId": 131,
              "userName": "test"
            },{
              "userId": 135,
              "userName": "123"
            }
          ]
    *
    * */
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesWorkforceConfig domain) {
        Assert.notNull(domain.getWorkforceId(), "workforceId不能为空！");
        boolean flag = baseService.updateWorkforceConfig(domain);
        return toAjax(flag);
    }

    @DeleteMapping("remove/{workforceId}")
    public AjaxResult remove(@PathVariable Long workforceId) {
        Assert.notNull(workforceId, "workforceId不能为空！");
        //baseService.removeBatchByIds(Arrays.asList(pkIds));
        if (baseService.removeWorkforceConfigByIds(workforceId)) {
            return success();
        } else {
            return error();
        }
    }

    @PostMapping("/page")
    public AjaxResult page(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size,@RequestHeader(value = "equipmentId") long equipmentId) {
       /* Page page = Page.of(current, size);
        Page<MesWorkforceConfig> list = baseService.page(page, Wrappers.query(domain));
        return success(list);*/
        PageHelper.startPage(current, size);
        QueryWrapper<MesWorkforceConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", "0");
        queryWrapper.eq("equipment_id", equipmentId);
        List<MesWorkforceConfig> list = baseService.list(queryWrapper);
        for (MesWorkforceConfig mesWorkforceConfig : list) {
            List<MesWorkforceXref> workforceXrefList = workforceXrefService.list(Wrappers.lambdaQuery(MesWorkforceXref.class).eq(MesWorkforceXref::getWorkforceId, mesWorkforceConfig.getWorkforceId()).eq(MesWorkforceXref::getDeleted, "0"));
            mesWorkforceConfig.setWorkforceXrefList(workforceXrefList);
        }
        PageInfo pageInfo = new PageInfo(list);
        return success(pageInfo);
    }

    /*@PostMapping("/export")
    public void export(HttpServletResponse response, MesWorkforceConfig domain) {
        List<MesWorkforceConfig> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesWorkforceConfig> util = new ExcelUtil<MesWorkforceConfig>(MesWorkforceConfig.class);
        util.exportExcel(response, list, "参数数据");
    }*/

    /**
     * 获取操作员角色的用户列表
     */
    @Operation(summary = "获取操作员角色的用户列表")
    @PostMapping("/getUserList")
    public AjaxResult getUserList() {
        //查询操作员角色的用户信息
        List<SysUser> userList = userService.getUserListByRole();
        return success(userList);
    }


}